package com.dingshi.security;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dingshi.security.api.ApiResult;
import com.dingshi.security.api.ApiReturnResultListener;
import com.dingshi.security.api.DialApi;
import com.dingshi.security.db.SettingLoader;
import com.dingshi.security.entity.ContactEntity;
import com.dingshi.security.util.Utils;
import com.dingshi.security.view.LoadDialog;

public class DailActivity extends BaseActivity implements OnClickListener{
	protected static final int ID_LOADING = 2013;
	private ListView mListView;
	private ArrayList<ContactEntity> list;
	private Button btn_back;
	private TextView title_text;
	private TextView btn_recoder;
	private TextView btn_common;
	private TextView btn_contact;
	private TextView tips;
	private ListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dail);
		
		initTopBar();
		initButton();
		
		tips = (TextView)findViewById(R.id.tips);
		mListView = (ListView)findViewById(R.id.listView1);
		mListView.setCacheColorHint(Color.TRANSPARENT);
		
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ContactEntity entity = list.get(arg2);
				Utils.dail(DailActivity.this,entity.getTelephone());
			}
		});
		
		getList();
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ID_LOADING:
			LoadDialog dialog = new LoadDialog(this, getResources().getString(R.string.login_loading));
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}
	private void initButton() {
		btn_recoder = (TextView)findViewById(R.id.textView1);
		btn_common = (TextView)findViewById(R.id.textView2);
		btn_contact = (TextView)findViewById(R.id.textView3);
		btn_recoder.setOnClickListener(this);
		btn_common.setOnClickListener(this);
		btn_contact.setOnClickListener(this);
		
	}

	private void initTopBar() {
		btn_back = (Button)findViewById(R.id.btn_back);
		title_text = (TextView)findViewById(R.id.title_text);
		btn_back.setOnClickListener(this);
		title_text.setText("拨号");
		
	}

	private ArrayList<ContactEntity> getList() {
		DialApi api = new DialApi(this);
		api.setReturnResultListener(new ApiReturnResultListener() {

			@Override
			public <T> void onReturnSucceedResult(int requestCode,
					ApiResult<T> apiResult) {
				JSONObject jsonEntity = apiResult.getJsonEntity();
				
				Log.e("aa", jsonEntity.toString());
				if (jsonEntity != null) {
					//{"d":[{"__type":"QuickDialReturn:r","c":"119","t":"火警"},{"__type":"QuickDialReturn:r","c":"110","t":"匪警"},{"__type":"QuickDialReturn:r","c":"120","t":"120"}],"t":1}

					try {
						int type = jsonEntity.getInt("t");
						switch (type) {
						case 0://对象
							break;
						case 1://数组
							JSONArray array = jsonEntity.getJSONArray("d");
							if(array!=null) {
								list = new ArrayList<ContactEntity>();
								ContactEntity entity = null;
								for(int i=0;i<array.length();i++) {
									entity = new ContactEntity();
									JSONObject opt = (JSONObject) array.opt(i);
									entity.setUsername(opt.getString("t"));
									entity.setTelephone(opt.getString("c"));
									entity.setType(opt.getString("s"));
									list.add(entity);
								}
							}
							updateList("1");
							break;
						case -1://错误
							break;

						default:
							break;
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
					dismissDialog(ID_LOADING);
				}
				
				
			}

			@Override
			public <T> void onReturnFailResult(int requestCode,
					ApiResult<T> apiResult) {
				dismissDialog(ID_LOADING);
			}

			@Override
			public <T> void onReturnLoading(int requestCode) {
				showDialog(ID_LOADING);
			}
		});
		api.getDialList(1, SettingLoader.getDepartNum(this),this);//小区编号
		
		return list;
	}
	private void updateList(String type) {
		if (list != null && list.size() > 0) {
			tips.setVisibility(View.GONE);
			mListView.setVisibility(View.VISIBLE);
			adapter = new ListAdapter(this, handleList(type,list));
			mListView.setAdapter(adapter);
		} else {
			tips.setVisibility(View.VISIBLE);
			mListView.setVisibility(View.GONE);
		}
	}

	private ArrayList<ContactEntity> handleList(String type,
			ArrayList<ContactEntity> list) {
		if(type == null) {
			return list;
		}
		ArrayList<ContactEntity> mList = new ArrayList<ContactEntity>();
		for(ContactEntity contact : list) {
			if(type.equals(contact.getType())) {
				mList.add(contact);
			}
		}
		return mList;
	}

	private final class ListAdapter extends BaseAdapter {
		private LayoutInflater inflater;
		private Context context;
		private ArrayList<ContactEntity> list;

		ListAdapter(Context context,ArrayList<ContactEntity> list) {
			this.context  = context;
			this.list = list;
			inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ContactEntity entity = list.get(position);
			if (entity == null) {
				return null;
			}
			final ViewHolder holder;
			
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.dail_item, null);
				holder = new ViewHolder();
				holder.headicon = (ImageView) convertView.findViewById(R.id.headicon);
				holder.username = (TextView) convertView.findViewById(R.id.username);
				holder.telephone = (TextView) convertView.findViewById(R.id.telephone);
				holder.createTime = (TextView) convertView.findViewById(R.id.createTime);
				holder.continueTime = (TextView) convertView.findViewById(R.id.continueTime);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			updateData(holder,entity);
			
			return convertView;
		}

		private void updateData(ViewHolder holder, ContactEntity entity) {
			holder.headicon.setImageResource(R.drawable.dail_default);
			holder.username.setText(entity.getUsername());
			holder.telephone.setText(entity.getTelephone());
//			holder.createTime.setText(ConvertTime(entity.getCreateTime()));
//			holder.continueTime.setText(entity.getContinueTime());
			
		}

		private String ConvertTime(long createTime) {
			Date date = new Date(createTime);
			SimpleDateFormat format = new SimpleDateFormat("hh:MM:ss");
			return format.format(date);
		}
	}
	static class ViewHolder {
		ImageView headicon;
		TextView username;
		TextView telephone;
		TextView createTime;
		TextView continueTime;
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			super.onBackPressed();
			break;
		case R.id.textView1:
			btn_recoder.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dail_click, 0, 0, 0);
			btn_common.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite_normal, 0, 0, 0);
			btn_contact.setCompoundDrawablesWithIntrinsicBounds(R.drawable.contact_normal, 0, 0, 0);
			btn_recoder.setTextColor(Color.WHITE);
			btn_common.setTextColor(Color.GRAY);
			btn_contact.setTextColor(Color.GRAY);
			updateList(null);
			break;
		case R.id.textView2:
			btn_recoder.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dail_normal, 0, 0, 0);
			btn_common.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite_click, 0, 0, 0);
			btn_contact.setCompoundDrawablesWithIntrinsicBounds(R.drawable.contact_normal, 0, 0, 0);
			btn_recoder.setTextColor(Color.GRAY);
			btn_common.setTextColor(Color.WHITE);
			btn_contact.setTextColor(Color.GRAY);
			updateList("2");
			break;
		case R.id.textView3:
			btn_recoder.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dail_normal, 0, 0, 0);
			btn_common.setCompoundDrawablesWithIntrinsicBounds(R.drawable.favorite_normal, 0, 0, 0);
			btn_contact.setCompoundDrawablesWithIntrinsicBounds(R.drawable.contact_click, 0, 0, 0);
			btn_recoder.setTextColor(Color.GRAY);
			btn_common.setTextColor(Color.GRAY);
			btn_contact.setTextColor(Color.WHITE);
			updateList("1");
			break;
		default:
			break;
		}
		
	}
}
